package practica1.ui.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.swing.*;
import practica1.Practica1;
import practica1.language.Language;

/**
 * JDialog que muestra la información Sobre de la aplicación
 * @author Miguel González - Ceura
 */
public class DialogSobre extends JDialog  {
    
    private JButton btnAceptar;
    
    /**
     * Constructor del JDialog
     * @param parent Frame sobre el que se va a localizar
     */
    public DialogSobre(Frame parent) {
        super(parent);
        
        init();
        
        setLocationRelativeTo(parent);
    }
    
    /**
     * Constructor del JDialog
     */
    public DialogSobre() {
        super();
        
        init();
        
        setLocationRelativeTo(null);
    }
    
    /**
     * Inicializa los componentes Swing del JDialog
     */
    private void init() {
        Container contentPane = getContentPane();
        setSize(500, 300);
        setTitle(Language.getI().getP("SOBRE"));
        setModal(true);
        contentPane.setLayout(new BorderLayout());
        
        
        JLabel lblSobre = new JLabel("Miguel González Gómez");
        
        InputStream is = null;
        BufferedReader br = null;
        try {
            is = DialogSobre.class.getResource
                    ("SobreContent.properties").openStream();
            br = new BufferedReader(new InputStreamReader(is));
            
            String linea;
            StringBuilder textoAbout = new StringBuilder();
            while((linea = br.readLine()) != null) {
                textoAbout.append(linea);
            }
            
            lblSobre.setText(textoAbout.toString());
        } catch (IOException ex) {
            Practica1.log.error("No se pudo cargar las propiedades del dialog sobre",
                    ex);
        } catch(java.lang.NullPointerException ex) {
            Practica1.log.error("No se pudo cargar las propiedades del dialog sobre",
                    ex);
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch(IOException ex) {
                    Practica1.log.error("Error cerrando br", ex);
                }
            }
            
            if(is != null) {
                try {
                    is.close();
                } catch(IOException ex) {
                    Practica1.log.error("Error cerrando br", ex);
                }
            }
        }
        
        lblSobre.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        lblSobre.setOpaque(true);
        lblSobre.setBackground(Color.WHITE);
        btnAceptar = new JButton(Language.getI().getP("ACEPTAR"));
        btnAceptar.addActionListener(new AceptarPulsado());
        
        //Centro
        JScrollPane scrollTexto = new JScrollPane(lblSobre);
        scrollTexto.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        contentPane.add(scrollTexto, BorderLayout.CENTER);
        
        //Sur
        contentPane.add(btnAceptar, BorderLayout.SOUTH);
    }
    
    /**
     * Evento que se produce cuando se pulsa Aceptar
     */
    private class AceptarPulsado implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
        }
    }
}
